package com.example.oasystem.service.impl;


import com.example.oasystem.dao.file.AoaFileListDao;
import com.example.oasystem.dao.file.AoaFilePathDao;
import com.example.oasystem.entity.file.AoaFileList;
import com.example.oasystem.entity.file.AoaFilePath;
import com.example.oasystem.entity.user.AoaUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FileManageServiceImpl {
    @Autowired
    AoaFileListDao fileListDao;
    @Autowired
    AoaFilePathDao aoaFilePathDao;
    public void rename(String name, Integer renamefp, Integer nowpathid, boolean isfile, AoaUser user){
        if(isfile){
            //文件名修改
            AoaFileList fl = fileListDao.selectOneFlist(renamefp);
            String newname = name+"."+ fl.getFileShuffix();
            //修改文件名字
            fileListDao.updateFileName(newname,renamefp);
        }else{
            //文件夹名修改
            AoaFilePath fp = fileListDao.findFpOne(nowpathid);
            //判斷文件夾是否存在
            if (fileListDao.selectFiles(name, fp.getParentId(),user.getUserId())==0){
                fileListDao.updateFilesName(name,renamefp);
            }
        }
    }

    /*public void moveAndcopy(List<Long> mcfileids,List<Long> mcpathids,Long topathid,boolean fromwhere,Long userid) {
        AoaFilePath topath = aoaFilePathDao.selectOne(new QueryWrapper<AoaFilePath>().eq("path_id",topathid));
        System.out.println("topath  "+topath);
        if(fromwhere){
            System.out.println("这里是移动！！~~");
            if(!mcfileids.isEmpty()){
                System.out.println("fileid is not null");
                for (Long mcfileid : mcfileids) {
                    AoaFileList filelist = fileListDao.selectOne(new QueryWrapper<AoaFileList>().eq("file_id",mcfileid));
                    filelist.setPathId(topath.getPathId());
                    //更改状态--
                    fileListDao.saveflistplus(filelist);
                }
            }
            if(!mcpathids.isEmpty()){
                System.out.println("pathid is not null");
                for (Long mcpathid : mcpathids) {
                    AoaFilePath filepath = aoaFilePathDao.selectOne(new QueryWrapper<AoaFilePath>().eq("path_id",mcpathid));
                    filepath.setParentId(topathid);
                    aoaFilePathDao.save(filepath);
                }
            }
        }else{
            System.out.println("这里是复制！！~~");
            if(!mcfileids.isEmpty()){
                System.out.println("fileid is not null");
                for (Long mcfileid : mcfileids) {
                    AoaFileList filelist = fileListDao.selectOne(new QueryWrapper<AoaFileList>().eq("file_id",mcfileid));
                    System.out.println("filelist"+filelist);

                    filelist.setFileId(0L);
                    filelist.setPathId(topath.getPathId());
                    filelist.setFileIstrash(0L);
                    filelist.setFileIsshare(0L);
                    fileListDao.insert(filelist);
                }
            }
            if(!mcpathids.isEmpty()){
                System.out.println("pathid is not null");
                for (Long mcpathid : mcpathids) {
                    copypath(mcpathid, topathid, true, userid);
                }
            }
        }
    }

    public void copypath(Long mcpathid,Long topathid,boolean isfirst,Long userid){
        AoaFilePath filepath = aoaFilePathDao.selectById(mcpathid);
        //第一个文件夹的复制
        AoaFilePath copypath = new AoaFilePath();
        copypath.setParentId(topathid);
        String copypathname = filepath.getPathName();
        if(isfirst){
            copypathname = "拷贝 "+filepath.getPathName().replace("拷贝 ", "");
        }
        copypath.setPathName(copypathname);
        copypath.setPathUserId(userid);
        aoaFilePathDao.save(copypath);
        copypath = aoaFilePathDao.findfPath(copypath);

        //这一个文件夹下的文件的复制
        List<AoaFileList> filelists = fileListDao.selectList(new QueryWrapper<AoaFileList>().eq("path_id",filepath)
                .eq("file_istrash",0L));
        for (AoaFileList fileList : filelists) {
            copyfile(fileList,copypath,false);
        }

        List<AoaFilePath> filepathsons = aoaFilePathDao.findpathByParent(filepath.getPathId(), userid);

        if(!filepathsons.isEmpty()){
            for (AoaFilePath filepathson : filepathsons) {
                copypath(filepathson.getPathId(),copypath.getPathId(),false,userid);
            }
        }
    }

    *//**
     * 文件复制
     * @param filelist
     *//*
    public void copyfile(AoaFileList filelist,AoaFilePath topath,boolean isfilein){
        String shuffix = filelist.getFileShuffix();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        AoaFileList filelist1 = new AoaFileList();
        String filename="";
        if(isfilein){
            filename = "拷贝 "+filelist.getFileName().replace("拷贝 ", "");
        }else{
            filename = filelist.getFileName();
        }
        filelist1.setFileName(filename);
        filelist1.setFilePath(filelist.getFilePath());
        filelist1.setFileShuffix(shuffix);
        filelist1.setSize(filelist.getSize());
        filelist1.setUploadTime(sdf.format(new Date()));
        filelist1.setPathId(topath.getPathId());
        filelist1.setContentType(filelist.getContentType());
        filelist1.setFileUserId(filelist.getFileUserId());
        fileListDao.saveflist(filelist1);
    }*/

}
